﻿using RpcClient.Attr;
using RpcClient.Interface;
using RpcClient.Model;
using RpcHelper;
using RpcModel;
using RpcModularModel.Visit.Model;

namespace RpcModular.Service
{
        [UnityName ("RpcVisitService")]
        internal class RpcVisitService : IExtendService
        {
                public string Name => "RpcVisitService";
                private ITcpRouteCollect _Route;
                private IRpcService _Service;
                public void Load (IRpcService service)
                {
                        this._Service = service;
                        service.StartUpComplate += this.Service_StartUpComplate;
                        this._Route = RpcClient.RpcClient.Unity.Resolve<ITcpRouteCollect> ();
                        VisitService.StatusRefresh = this._StatusRefresh;
                        if (VisitService.Config.IsEnable)
                        {
                                service.ReceiveEnd += this.Service_ReceiveEnd;
                        }
                }

                private void _StatusRefresh (bool isEnable)
                {
                        if (isEnable)
                        {
                                _Service.ReceiveEnd += this.Service_ReceiveEnd;
                        }
                        else
                        {
                                _Service.ReceiveEnd -= this.Service_ReceiveEnd;
                        }
                }

                private void Service_StartUpComplate ()
                {
                        this._Route.RegRouteEvent += this._Route_RegRouteEvent; ;
                        IRoute[] routes = this._Route.GetRoutes ();
                        if (routes.Length > 0)
                        {
                                RpcVisit[] list = routes.ConvertAll (a => new RpcVisit
                                {
                                        Dictate = a.RouteName,
                                        Show = a.RouteShow
                                });
                                if (list.Length > 0)
                                {
                                        VisitService.Regs (list);
                                }
                        }
                }

                private void _Route_RegRouteEvent (IRoute obj)
                {
                        VisitService.Reg (new RpcVisit
                        {
                                Dictate = obj.RouteName,
                                Show = obj.RouteShow
                        });
                }

                private void Service_ReceiveEnd (IMsg msg, TcpRemoteReply reply)
                {
                        if(msg.MsgKey == "AddVisitLog" && RpcClient.RpcClient.SystemTypeVal== "sys.sync")
                        {
                                return;
                        }
                        else if (reply != null && reply.IsError)
                        {
                                VisitService.Failure (msg.MsgKey);
                        }
                        else
                        {
                                VisitService.Success (msg.MsgKey);
                        }
                }
        }
}
